package com.ioioioio.pojo;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * ajax请示返回的对象
 */
public class ReturnMsg {
    private List<?> rows;
    private int total;
    private String rspcod;
    private String rspmsg;
    private String url;
    private Object obj;

    public ReturnMsg() { }

    public Map<String, Object> toMap() throws Exception {
        Class<? extends Object> type = this.getClass();
        Map<String, Object> map = new HashMap<String, Object>();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!"class".equals(propertyName)) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(this, new Object[0]);
                if (result != null) {
                    map.put(propertyName, result);
                }
            }
        }
        return map;
    }

    public ReturnMsg(String rspcod, String rspmsg) {
        this.rspcod = rspcod;
        this.rspmsg = rspmsg;
    }

    public Object getObj() {
        return this.obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public String getRspcod() {
        return this.rspcod == null ? "200" : this.rspcod;
    }

    public void setRspcod(String rspcod) {
        this.rspcod = rspcod;
    }

    public String getRspmsg() {
        return this.rspmsg == null ? "失败" : this.rspmsg;
    }

    public void setRspmsg(String rspmsg) {
        this.rspmsg = rspmsg;
    }

    public String getUrl() {
        return this.url == null ? "" : this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public ReturnMsg setFail() {
        setRspcod("201");
        return this;
    }

    public ReturnMsg setFail(String msg) {
        setRspcod("201");
        setRspmsg(msg);
        return this;
    }

    public ReturnMsg setFail(String msg, String url) {
        setRspcod("201");
        setRspmsg(msg);
        setUrl(url);
        return this;
    }

    public ReturnMsg setFail(String msg, Map<?, ?> data) {
        setRspcod("201");
        setRspmsg(msg);
        return this;
    }

    public ReturnMsg setFail(String msg, String url, Map<?, ?> data) {
        setRspcod("201");
        setRspmsg(msg);
        setUrl(url);

        return this;
    }

    public ReturnMsg setSuccess() {
        setRspcod("200");
        return this;
    }

    public ReturnMsg setSuccess(String msg) {
        setRspcod("200");
        setRspmsg(msg);
        return this;
    }

    public ReturnMsg setSuccess(String msg, String url) {
        setRspcod("200");
        setRspmsg(msg);
        setUrl(url);
        return this;
    }

    public ReturnMsg setSuccess(String msg, Map<?, ?> data) {
        setRspcod("200");
        setRspmsg(msg);

        return this;
    }

    public ReturnMsg setSuccess(String msg, String url, Map<?, ?> data) {
        setRspcod("200");
        setRspmsg(msg);
        setUrl(url);

        return this;
    }

    public int getTotal() {
        return this.total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<?> getRows() {
        return this.rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }

    public void setMsg(String rspcod, String rspmsg) {
        this.rspcod = rspcod;
        this.rspmsg = rspmsg;
    }
}